﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class CondFmtRecord : CondFmtRecordBase
    {
        private readonly List<CFRecord> _cfRecords = new List<CFRecord>();
        private readonly SqRefU sqRefU = new SqRefU();
        private Ref8U refBound;
        private ushort ccf;
        private readonly List<CFExRecord> _cfExRecords = new List<CFExRecord>();
        private bool fToughRecalc = false;
        private ushort nId;
        private ushort Anid
    {
        get
        {
            ushort result = (ushort) (nId << 1);
            if (this.fToughRecalc)
                result |= 0x01;
            return result;
        }
    }

        public CondFmtRecord(ConditionalFormatting conditionalFormatting, ushort id)
        {
            Contract.Requires(id > 0);
            this.ccf = (ushort) conditionalFormatting.Rules.Count;
            this.nId = id;
            refBound = (Ref8U) conditionalFormatting.Range.Address;
            this.sqRefU =
                SqRefU.FromRangeAddress(Enumerable.Select(conditionalFormatting.Range.GetSimpleRanges(), r => r.Address));
        }
        public ushort Id
        {
            get
            {
                return nId;
            }
        }

        public CondFmtRecord()
        {
        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.CondFmt; }
        }

        public List<CFRecord> CFRecords
        {
            get { return _cfRecords; }
        }

        public List<CFExRecord> CfExRecords
        {
            get { return _cfExRecords; }
        }


      /*  internal override void Apply(BiffReaderWorksheetContext context)
        {
            context.AddCondFmtRecord(this);
            context.WorkbookContext.GetActions(ActionType.ConditionalFormatting).Add(
                obj => context.Worksheet.AddConditionalFormatting(ToConditionalFormatting(context)));

            //context.CurrentConditionalFormatting = ToConditionalFormatting(context.Worksheet);
        }*/

        protected override ConditionalFormatting ToConditionalFormatting( BiffReaderWorksheetContext context)
        {
            var result = new ConditionalFormatting();
            result.Range = sqRefU.ToRange(context.Worksheet);
            int index = 0;
            foreach (var r in CFRecords)
            {
                CFExRecord cfExRecord = null;
                if (CfExRecords.Count > index)
                {
                    cfExRecord = CfExRecords[index];
                }
                result.Rules.Add(CFRecord.ToRule(context, result.Range.TopLeftCell, r, cfExRecord));
                index++;
            }
            return result;
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            context.Write(ccf);

            context.Write(this.Anid);
            refBound.Write(context.Writer);
            sqRefU.Write(context.Writer);
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            this.ccf = context.ReadUInt16();
           
            var anid = context.ReadUInt16();
            this.fToughRecalc = ((anid & 0x01) != 0);
            this.nId = (ushort)(anid >> 1);
            Contract.Assert(this.Anid == anid);

            refBound.Read(context.Reader);
            sqRefU.Read(context.Reader);
        }
    }
}